package com.android.dwt.data;

import com.android.dwt.ListItem;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DBListAdapter {
	public static final String KEY_ID = "_id";
	public static final String KEY_NAME = "list_name";
	public static final String KEY_NOTE = "list_note";
	public static final int COL_ID = 0;
	public static final int COL_LIST_NAME = 1;
	public static final int COL_LIST_NOTE = 2;
	
	private final String TABLE_NAME = "list";
	private SQLiteDatabase db;

	
	public DBListAdapter(SQLiteDatabase db) {
		this.db = db;
	}

	/*
	 * Insert a new item
	 */
	public long insertList(ListItem _list) {
		// Create a new row of values to insert.
		ContentValues newListVal = new ContentValues();
		// Assign values for each row.
		newListVal.put(KEY_NAME, _list.getListName());
		newListVal.put(KEY_NOTE, _list.getListNote());

		return db.insert(TABLE_NAME, null, newListVal);
	}

	/*
	 * Remove a list based on its index
	 */
	public boolean removeList(long _rowIndex) {
		return db.delete(TABLE_NAME, KEY_ID + "=" + _rowIndex, null) > 0;
	}
	
	/*
	 * Remove a list based on its index
	 */
	public boolean removeList(String list) {
		String tmp = KEY_NAME + "=\"" + list + "\"";
		return db.delete(TABLE_NAME, tmp, null) > 0;
	}
	
	/*
	 * Remove all lists
	 */
	public boolean removeAllList() {
		return db.delete(TABLE_NAME, null, null) > 0;
	}

	/*
	 * Update a list
	 */
	public boolean updateList(long _rowIndex, ListItem _list) {
		ContentValues newValue = new ContentValues();
		newValue.put(KEY_NAME, _list.getListName());
		newValue.put(KEY_NOTE, _list.getListNote());
		return db.update(TABLE_NAME, newValue, KEY_ID + "=" + _rowIndex, null) > 0;
	}
	
	/*
	 * Update a list
	 */
	public boolean updateList(String str, ListItem _list) {
		ContentValues newValue = new ContentValues();
		newValue.put(KEY_NAME, _list.getListName());
		newValue.put(KEY_NOTE, _list.getListNote());
		return db.update(TABLE_NAME, newValue, KEY_NAME + "=\"" + str + "\"", null) > 0;
	}

	/*
	 * Cursor operations
	 */
	public Cursor getAllListItems() {
//		return db.query(TABLE_NAME, new String[] { KEY_ID, KEY_NAME,
//				KEY_NOTE}, null, null, null, null, null);
		return db.query(TABLE_NAME, null, null, null, null, null, null);
	}

	public Cursor setCursorToListItem(long _rowIndex) throws SQLException {
		Cursor result = db.query(true, TABLE_NAME, new String[] { KEY_ID, KEY_NAME,
				KEY_NOTE }, KEY_ID + "=" + _rowIndex, null, null, null, null,
				null);
		if ((result.getCount() == 0) || !result.moveToFirst()) {
			throw new SQLException("No lists found for row: " + _rowIndex);
		}
		return result;
	}
	
	public long getRowId(ListItem li) {
		Cursor rst;
		
		rst = db.query(true, TABLE_NAME, new String[] { KEY_ID, KEY_NAME,
				KEY_NOTE }, KEY_NAME + "=\"" + li.getListName() + "\"", null, null, null, null,
				null);
		if ((rst.getCount() == 0) || !rst.moveToFirst()) {
			throw new SQLException("No lists found for: " + li.getListName());
		}
		
		return rst.getColumnIndex(KEY_ID);
	}

	public ListItem getListItem(long _rowIndex) throws SQLException {
		
		Cursor cursor = db.query(true, TABLE_NAME,
				new String[] { KEY_ID, KEY_NAME, KEY_NOTE }, KEY_ID + "=" + _rowIndex,
				null, null, null, null, null);
		
		if ((cursor.getCount() == 0) || !cursor.moveToFirst()) {
			throw new SQLException("No to do item found for row: " + _rowIndex);
		}
		
		String list = cursor.getString(COL_LIST_NAME);
		String note = cursor.getString(COL_LIST_NOTE);
		ListItem result = new ListItem(list, note);
		
		cursor.close();
		
		return result;
	}

}